package DataStr.homework04.h2;

public class BubbleSort {
    public static void BubbleSort(int []arr){
        int n=arr.length;
        //外循环
        for(int i=0;i<n-1;i++){
            boolean flag=false;//用来判断内循环是否有无交换
            for(int j=0;j<n-i-1;j++){
                if(arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
                flag=true;
            }
            if(!flag){
                break;
            }
        }
    }
    public static void main(String[]args){
        int []arr={49,38,65,97,76,13,27,49};
        System.out.print("冒泡排序之前排序为:");
        for(int num:arr){
            System.out.print(num+" ");
        }
        System.out.println();
        BubbleSort(arr);
        System.out.print("冒泡排序之后排序为:");
        for(int num:arr){
            System.out.print(num+" ");
        }
    }
}
